package com.chenfan.magic.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.chenfan.magic.utils.AES;
import com.chenfan.magic.utils.StringKit;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;
import java.util.Date;
/**
 * @author lm
 */
@Data
@TableName("idc_scripts_publish")
@ApiModel(value = "ScriptsPublish对象", description = "ScriptsPublish对象")
public class IdcScriptsPublish implements Serializable {

    private static final long serialVersionUID = 1L;

    private static final String ENCRYPT_KEY = "$62a51ed179969dd3af28434b2e967913";

    @TableId(value = "sp_script_id", type = IdType.INPUT)
    @ApiModelProperty(value = "id")
    private String spScriptId;

    @ApiModelProperty(value = "脚本名称")
    private String spScriptName;

    @ApiModelProperty(value = "脚本描述")
    private String spScriptDescription;

    @ApiModelProperty(value = "脚本上下文")
    private String spScriptContext;

    @ApiModelProperty(value = "请求 REST 类型")
    private String spScriptMethod;

    @ApiModelProperty(value = "令牌箭")
    private String spScriptTokenArrows;

    @ApiModelProperty(value = "预请求组")
    private String spScriptPreRequestParams;

    @ApiModelProperty(value = "预响应组")
    private String spScriptPreResponseParams;

    @ApiModelProperty(value = "文档")
    private String spScriptApiDocument;

    @ApiModelProperty(value = "发布版本号")
    private String spScriptPublishVersion;

    @ApiModelProperty(value = "发布时间")
    private Date spScriptPublishTime;

    @ApiModelProperty(value = "发布模式")
    private String spScriptPublishMode;

    @ApiModelProperty(value = "灰度用户列表")
    private String spScriptGrayUnionIds;

    @ApiModelProperty(value = "是否需要授权")
    private String spScriptIsNeedToken;

    @ApiModelProperty(value = "路由")
    private String spScriptRoute;

    @ApiModelProperty(value = "应用 Key")
    private String spScriptAppId;

    @ApiModelProperty(value = "定时表达式")
    private String spScriptScheduleCron;

    @ApiModelProperty(value = "索引")
    private String spScriptIndex;

    @ApiModelProperty(value = "解析引擎")
    private String spScriptResolveEngine;

    @ApiModelProperty(value = "缓存开关")
    private Integer spScriptIsCache;

    @ApiModelProperty(value = "缓存级别")
    private Integer spScriptIsCacheLevel;

    @ApiModelProperty(value = "拦截器组")
    private String spScriptFilters;

    @ApiModelProperty(value = "排序")
    private Integer spScriptSort;

    @ApiModelProperty(value = "拦截器标识")
    private Integer spScriptIsFilter;

    public String getSpScriptContextDecrypt(String sercet) {
        if (StringKit.isNotBlank(spScriptContext) && spScriptContext.trim().startsWith(ENCRYPT_KEY)) {
            spScriptContext = AES.decrypt(spScriptContext.replace(ENCRYPT_KEY, ""), sercet);
        }
        return spScriptContext;
    }

    public void setSpScriptContextEncrypt(String spScriptContext, String sercet) {
        if (StringKit.isNotBlank(spScriptContext) && !spScriptContext.trim().startsWith(ENCRYPT_KEY)) {
            spScriptContext = ENCRYPT_KEY + AES.encrypt(spScriptContext, sercet);
        }
        this.spScriptContext = spScriptContext;
    }

    public IdcScriptsPublish() {

    }

    public IdcScriptsPublish(IdcScripts idcScripts) {
        this.spScriptId = idcScripts.getId();
        this.spScriptName = idcScripts.getName();
        this.spScriptDescription = idcScripts.getDescription();
        this.spScriptContext = idcScripts.getContext();
        this.spScriptMethod = idcScripts.getMethod();
        this.spScriptTokenArrows = idcScripts.getTokenArrows();
        this.spScriptPreRequestParams = idcScripts.getPreRequestParams();
        this.spScriptPreResponseParams = idcScripts.getPreResponseParams();
        this.spScriptApiDocument = idcScripts.getApiDocument();
        this.spScriptPublishVersion = idcScripts.getPublishVersion();
        this.spScriptPublishTime = new Date();
        this.spScriptPublishMode = idcScripts.getPublishMode();
        this.spScriptGrayUnionIds = idcScripts.getGrayUnionIds();
        this.spScriptIsNeedToken = idcScripts.getIsNeedToken();
        this.spScriptRoute = idcScripts.getRoute();
        this.spScriptAppId = idcScripts.getAppId();
        this.spScriptScheduleCron = idcScripts.getScheduleCron();
        this.spScriptIndex = idcScripts.getIndez();
        this.spScriptResolveEngine = idcScripts.getResolveEngine();
        this.spScriptIsCache = idcScripts.getIsCache();
        this.spScriptIsCacheLevel = idcScripts.getIsCacheLevel();
        this.spScriptFilters = idcScripts.getFilters();
        this.spScriptSort = idcScripts.getSort();
        this.spScriptIsFilter = idcScripts.getIsFilter();
    }


}
